//
//  InputControlM.m
//  
//  jhf
//
//  Created by Mac on 2020/7/21.
//

#import "InputControlM.h"
#import <CooperSDK/UIColor+BFPaperColors.h>
#import <CooperSDK/UITextView+PlaceHolder.h>

@implementation InputControlM

- (void)controlTextField:(UITextField *)inputV; {
    [self controlV:inputV];
    /// 具体值 显示
    inputV.text = self.textStr;
    inputV.textColor = [UIColor colorWithHexString:self.textColorHex];
    inputV.font = self.textStrFont;
    inputV.textAlignment = self.textTextAlignment;
    
    inputV.keyboardType = self.keyboardType;
    
    /// 占位 显示
    if (self.placeholderStr) {
        if (self.placeholderColorHex && self.placeholderFont) {
            NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:self.placeholderStr
                                                                                                  attributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:self.placeholderColorHex],
                                                                                                               NSFontAttributeName : self.placeholderFont}];
            inputV.attributedPlaceholder = placeholderString;
        }else {
            inputV.placeholder = self.placeholderStr;
        }
    }
    
    /// 配置
    inputV.enabled = self.editEnable;
    inputV.clearButtonMode = self.clearModel;
    inputV.secureTextEntry = self.secureTextEntry;
    if (self.inputView) {
        inputV.inputView = self.inputView;
    }else {
        inputV.inputView = nil;
    }
}



- (void)controlTextView:(UITextView *)inputV; {
    [self controlV:inputV];
    /// 具体值 显示
    inputV.text = self.textStr;
    inputV.textColor = [UIColor colorWithHexString:self.textColorHex];
    inputV.font = self.textStrFont;
    inputV.textAlignment = self.textTextAlignment;
    
    inputV.keyboardType = self.keyboardType;
    
    [inputV addPlaceHolder:self.placeholderStr];
    
    if (self.placeholderColorHex) {
        inputV.placeHolderTextView.textColor = [UIColor colorWithHexString:self.placeholderColorHex];
    }
    if (self.placeholderFont) {
        inputV.placeHolderTextView.font = self.placeholderFont;
    }
    if (self.textStr && self.textStr.length > 0) {
        inputV.placeHolderTextView.hidden = true;
    }
    inputV.scrollEnabled = self.scrollEnabled;
}


+ (InputControlM *)defaultInputText:(NSString *)text; {
    InputControlM *model = [[InputControlM alloc] init];
    
    model.textTextAlignment = NSTextAlignmentLeft;
    
    /// 显示
    model.textStr = text;
    model.textStrFont = [UIFont systemFontOfSize:15];
    model.textColorHex = @"#333333";
    
    /// 占位
    //  model.placeholderStr = [NSString stringWithFormat:@"请输入%@", text ?: @""];
    model.placeholderStr = @"请输入";
    model.placeholderFont = [UIFont systemFontOfSize:15];
    model.placeholderColorHex = @"#aaaaaa";
    
    /// 是否可编辑
    model.editEnable = true;
    model.scrollEnabled = true;
    
    return model;
}


//MARK: -数字
- (void)setIsMoney:(BOOL)isMoney {
    _isMoney = isMoney;
    if (isMoney) {
        self.allowStrings = VERIFY_ALL_NUM_POINT;
        self.keyboardType = UIKeyboardTypeDecimalPad;
        self.moneyRightMaxLength = 2;
    }else {
        self.allowStrings = @"";
        self.keyboardType = UIKeyboardTypeDefault;
    }
}

//MARK: -手机号
- (void)setIsPhoneNumber:(BOOL)isPhoneNumber {
    _isPhoneNumber = isPhoneNumber;
    if (isPhoneNumber) {
        self.maxLength = 11;
        self.keyboardType = UIKeyboardTypeNumberPad;
        self.allowStrings = VERIFY_ALL_NUM;
    }else {
        self.maxLength = -1;
        self.keyboardType = UIKeyboardTypeDefault;
        self.allowStrings = @"";
    }
}

//MARK: -银行卡
- (void)setIsBankCardNumber:(BOOL)isBankCardNumber {
    _isBankCardNumber = isBankCardNumber;
    if (isBankCardNumber) {
        self.isNumber = true;
        self.maxLength = 19;
    }else {
        self.isNumber = false;
        self.maxLength = -1;
    }
}

//MARK: -纯数字
- (void)setIsNumber:(BOOL)isNumber {
    _isNumber = isNumber;
    if (isNumber) {
        self.keyboardType = UIKeyboardTypeNumberPad;
        self.allowStrings = VERIFY_ALL_NUM;
    }else {
        self.allowStrings = @"";
        self.keyboardType = UIKeyboardTypeDefault;
    }
}


//MARK: -身份证
- (void)setIsIdCard:(BOOL)isIdCard {
    _isIdCard = isIdCard;
    if (isIdCard) {
        self.maxLength = 18;
        self.allowStrings = VERIFY_ALL_NUM_X;
    }else {
        self.maxLength = -1;
        self.allowStrings = @"";
    }
}

// 数字和字母
- (void)setIsAlphaNum:(BOOL)isAlphaNum {
    _isAlphaNum = isAlphaNum;
    if (isAlphaNum) {
        self.allowStrings = VERIFY_ALL_ALPHA_NUM;
        self.keyboardType = UIKeyboardTypeASCIICapable;
    }else {
        self.allowStrings = @"";
        self.keyboardType = UIKeyboardTypeDefault;
    }
}

// 邮箱
- (void)setIsEmail:(BOOL)isEmail {
    _isEmail = isEmail;
    if (isEmail) {
        self.allowStrings = [NSString stringWithFormat:@"%@.@", VERIFY_ALL_ALPHA_NUM];
        self.keyboardType = UIKeyboardTypeASCIICapable;
    }else {
        self.allowStrings = @"";
        self.keyboardType = UIKeyboardTypeDefault;
    }
}

- (void)setIsPwd:(BOOL)isPwd {
    _isPwd = isPwd;
    self.isAlphaNum = isPwd;
    self.secureTextEntry = isPwd;
}


- (void)setInputType:(CooperInputType)inputType {
    _inputType = inputType;
    switch (inputType) {
        case CooperInputTypeDefault:
            break;
        case CooperInputTypeNumber:
            self.isNumber = true;
            break;
        case CooperInputTypeMoney:
            self.isMoney = true;
            break;
        case CooperInputTypeIdCard:
            self.isIdCard = true;
            break;
        case CooperInputTypePhoneNumber:
            self.isPhoneNumber = true;
            break;
        default:
            break;
    }
}

@end
